

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class RectangleTool extends Tool {
   
  protected Point startingMousePosition;
  protected Point currentMousePosition;
  protected Color saveColor;
  
  public RectangleTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
	  super(canvas, ShapeObjects, mdUser);
  }
  
  protected void drawRectangle(int x, int y, int width, int height) {
    int rectX = x;
    int rectY = y;
    int rectWidth = width;
    int rectHeight = height;
    if (width < 0) {
      rectX = x + width;
      rectWidth = -width;
    } 
    if (height < 0) {
      rectY = y + height;
      rectHeight = -height;
    }
    Graphics iBGraphics = canvas.getImageBufferGraphics();
    iBGraphics.drawRect(rectX, rectY, rectWidth, rectHeight);
      
    canvas.repaint();
  }

 public void mousePressed(MouseEvent e)  {
   /* Establish starting point for next drawing */
   startingMousePosition = e.getPoint();
   currentMousePosition = startingMousePosition;
   Graphics iBGraphics = canvas.getImageBufferGraphics();
   saveColor = iBGraphics.getColor( );
   iBGraphics.setXORMode(Color.lightGray);
   iBGraphics.setColor(Color.white);
   drawRectangle(startingMousePosition.x,
		 startingMousePosition.y, 1,1);
						
 }

 public void mouseDragged(MouseEvent e)  {
   Point newMousePosition = e.getPoint();
   drawRectangle(startingMousePosition.x, startingMousePosition.y,
         	currentMousePosition.x -startingMousePosition.x +1,
		currentMousePosition.y - startingMousePosition.y +1);
   drawRectangle(startingMousePosition.x,startingMousePosition.y,
                 newMousePosition.x -startingMousePosition.x + 1,
		 newMousePosition.y- startingMousePosition.y + 1);
   currentMousePosition = newMousePosition;
 }


  public void mouseReleased(MouseEvent e) { 
	Point newMousePosition = e.getPoint();
	drawRectangle(startingMousePosition.x,startingMousePosition.y,
	                 newMousePosition.x -startingMousePosition.x + 1,
	                 newMousePosition.y- startingMousePosition.y + 1);
    Graphics iBGraphics = canvas.getImageBufferGraphics();
	ShapeObjects.add(new RectangleObject(startingMousePosition, currentMousePosition, saveColor));	
	iBGraphics.setPaintMode();
	paintObjects(0);
	iBGraphics.setColor(saveColor);
  }
	public void keyPressed(KeyEvent e) {}
}
